package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.Orders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Server;
import com.ruoyi.system.service.IServerService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 服务器列表Controller
 *
 * @author ruoyi
 * @date 2024-12-23
 */
@RestController
@RequestMapping("/server")
@Slf4j
public class ServerController extends BaseController
{
    @Autowired
    private IServerService serverService;
    @Autowired
    private StringRedisTemplate
     stringRedisTemplate;

    /**
     * 查询服务器列表列表
     */
    @RequiresPermissions("system:server:list")
    @GetMapping("/list")
    public TableDataInfo list(Server server)
    {
        //按图一，列表数据展示正确（5分）
        //数据列表可以分页查询（3分）
        //在服务器列表左上角，展示登录用户的信息。（5分）
        //点击购买链接跳转到购买服务器页面（4分）
        //在服务器列表点击购买链接，跳转该页面，服务器信息回显正确。（4分）
        //购买时间只读，默认为系统当前时间。（4分）
        startPage();
        List<Server> list = serverService.selectServerList(server);
        stringRedisTemplate.opsForValue().set("list",list.toString());
        log.info("列表"+list);
        return getDataTable(list);
    }

    /**
     * 导出服务器列表列表
     */
    @RequiresPermissions("system:server:export")
    @Log(title = "服务器列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Server server)
    {
        List<Server> list = serverService.selectServerList(server);
        ExcelUtil<Server> util = new ExcelUtil<Server>(Server.class);
        util.exportExcel(response, list, "服务器列表数据");
    }

    /**
     * 获取服务器列表详细信息
     */




















    @RequiresPermissions("system:server:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(serverService.selectServerById(id));
    }

    /**
     * 新增服务器列表
     */
    @RequiresPermissions("system:server:add")
    @Log(title = "服务器列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Server server)
    {
        String username = SecurityUtils.getUsername();
        return toAjax(serverService.insertServer(server));
    }

    /**
     * 修改服务器列表
     */
    @RequiresPermissions("system:server:edit")
    @Log(title = "服务器列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Server server)
    {
        return toAjax(serverService.updateServer(server));
    }

    /**
     * 删除服务器列表
     */
    @RequiresPermissions("system:server:remove")
    @Log(title = "服务器列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(serverService.deleteServerByIds(ids));
    }

//    @GetMapping("/orderLList")
//    public R orderLList(){
////        Date date = new Date();
////
////        Date date1 = DateUtils.dateTime("yyyyMMdd", "20241201");
////        Date date2 = DateUtils.dateTime("yyyyMMdd", "20241224");
////        String key="ordersRedis";
////        stringRedisTemplate.opsForSet().add(key, JSON.toJSONString(Orders.builder().))
//        List<Orders> list = serverService.orderList();
//        list.stream().findAny()
//        stringRedisTemplate.opsForZSet().add("订单",JSON.toJSONString(list),list.getProduct_price());
//        log.info("列表"+list);
//        System.out.println("1");
//
//        return R.ok(list);
//    }
@PostMapping("/orderList")
public AjaxResult redis() {
    Date date = new Date();

    List<Orders> ordersList = serverService.orderList();
    for (Orders orders : ordersList) {
        stringRedisTemplate.opsForZSet().add("订单", JSON.toJSONString(orders), orders.getProduct_price());
        log.info("存储订单信息：{}", orders);
    }
    return null;
}




    @GetMapping("/redis")
    public R testRedis() throws Exception
    {

        String key = "scoreRankings";
        this.stringRedisTemplate.opsForZSet().add(key,"zhansan3",90);
        this.stringRedisTemplate.opsForZSet().add(key,"lisi",85);
        this.stringRedisTemplate.opsForZSet().add(key,"wangwu2",97);

        this.stringRedisTemplate.opsForZSet().add(key,"zhansan",89);
        this.stringRedisTemplate.opsForZSet().add(key,"lisi",67);
        this.stringRedisTemplate.opsForZSet().add(key,"wangwu",34);

        this.stringRedisTemplate.opsForZSet().add(key,"zhansan",67);
        this.stringRedisTemplate.opsForZSet().add(key,"lisi",87);
        this.stringRedisTemplate.opsForZSet().add(key,"wangwu",23);

        this.stringRedisTemplate.opsForZSet().add(key,"zhansan",66);
        this.stringRedisTemplate.opsForZSet().add(key,"lisi",77);
        this.stringRedisTemplate.opsForZSet().add(key,"wangwu",89);

        this.stringRedisTemplate.opsForZSet().add(key,"zhansan",23);
        this.stringRedisTemplate.opsForZSet().add(key,"lisi",45);
        this.stringRedisTemplate.opsForZSet().add(key,"wangwu1",99);

        this.stringRedisTemplate.opsForZSet().add(key,"zhansan",87);
        this.stringRedisTemplate.opsForZSet().add(key,"lisi",43);
        this.stringRedisTemplate.opsForZSet().add(key,"wangwu",56);

        // Set<String> set = this.stringRedisTemplate.opsForZSet().r

        Set<String> set = this.stringRedisTemplate.opsForZSet().range(key,0,-1);

        Set<String> set2 = this.stringRedisTemplate.opsForZSet().rangeByScore(key,89,98);

        long count = this.stringRedisTemplate.opsForZSet().zCard(key);

        Set set3 = this.stringRedisTemplate.opsForZSet().popMax(key,3);

        long count1 = this.stringRedisTemplate.opsForZSet().zCard(key);
        log.info("count1:{} count:{} ",count1,count);





        log.info("set:"+ JSON.toJSONString(set));
        log.info("set2:"+ JSON.toJSONString(set2));
        log.info("set3:"+ JSON.toJSONString(set3));







        return  R.ok();
    }





}
